#include "serialanalysedata.h"


SerialAnalyseData::SerialAnalyseData(QWidget *parent, M_SERIAL_PROTOCOL_TYPE type) : QWidget(parent)
{
    switch (type)
    {
        case m_otdr_opm_protocol:
            otdr_opm_layout();
        break;
        default:
         break;
    }
}
void SerialAnalyseData::otdr_opm_layout()
{
//    QGroupBox *opmParamGroupBox = new QGroupBox(this);
//    opmParamGroupBox->setTitle("光功率参数设置");
//    opmParamGroupBox->resize(400,300);
//    opmParamGroupBox->move(20,80);

    sample_rate_edit_group_box = new QGroupBox (this);
    sample_rate_edit_group_box->setTitle("采样速率");
    QStringList sampleRateStrList;
    sampleRateStrList<<"1sps"<<"5sps"<<"10sps"<<"100sps";
    for(int i = 0; i<sampleRateStrList.count();i++)
    {
        QRadioButton *radioBtn = new QRadioButton(sample_rate_edit_group_box);
        radioBtn->setText(sampleRateStrList.at(i));
        sample_rate_radio_btn_list.append(radioBtn);
        radioBtn->move(i*100+10,25);
    }
    sample_rate_edit_group_box->move(20,80);
    sample_rate_edit_group_box->resize(300,50);


    out_range_edit_group_box = new QGroupBox (this);
    out_range_edit_group_box->setTitle("输出量程");
    QStringList outRangeStrList;
    outRangeStrList<<"自动"<<"10~0dBm"<<"0~-10dBm"<<"-10~-20dBm"<<"-20~-30dBm"<<"-30~-40dBm"<<"-40~-50dBm"<<"-50~-60dBm"<<"-60~-70dBm";
    for(int i = 0; i<outRangeStrList.count();i++)
    {
        QRadioButton *radioBtn = new QRadioButton(out_range_edit_group_box);
        radioBtn->setText(outRangeStrList.at(i));
        out_range_radio_btn_list.append(radioBtn);
        radioBtn->move(i*100+10,25);
    }
    out_range_edit_group_box->move(320,80);
    out_range_edit_group_box->resize(600,50);


    wave_edit_group_box = new QGroupBox (this);
    wave_edit_group_box->setTitle("光功率校准");
    QStringList waveStrList;
    waveStrList<<"1310nm"<<"1550nm"<<"1490nm"<<"1625nm"<<"1650nm"<<"850nm"<<"1300nm";
    for(int i = 0; i<waveStrList.count();i++)
    {
        QRadioButton *radioBtn = new QRadioButton(wave_edit_group_box);
        radioBtn->setText(waveStrList.at(i));
        wave_radio_btn_list.append(radioBtn);
        radioBtn->move(i*100+10,25);
    }
    wave_edit_group_box->move(20,300);
    wave_edit_group_box->resize(600,50);

    line_edit_calibrate_value = new QLineEdit(this);
    line_edit_calibrate_value->move(650,320);
    line_edit_calibrate_value->resize(80,30);


    QStringList btnStrList;
    btnStrList<<"联机"<<"断开"<<"光功率设置"<<"读取光功率"<<"获取一次光功率值"<<"连续读取光功率值"\
             <<"停止"<<"设置校准值"<<"光源打开"<<"光源关闭"<<"设置光源功率";
    //<<"读取网线线序"<<"读取网线线长"<<"寻线功能开启"<<"寻线功能关闭";
    for(int i = 0; i< btnStrList.count();i++)
    {
        QPushButton * btn = new QPushButton(this);
        btn->setText(btnStrList.at(i));
        btn_list.append(btn);
        connect(btn,SIGNAL(released()),this,SLOT(opm_btn_handle()),Qt::AutoConnection);//
    }
    btn_list.at(0)->move(20,20);
    btn_list.at(1)->move(280,20);

    QFrame *line1 = new QFrame(this);
    line1->setGeometry(QRect(0, 55, 1024, 2));
    line1->setFrameShape(QFrame::HLine);
    line1->setFrameShadow(QFrame::Sunken);
    line1->raise();


    btn_list.at(2)->move(640,140);
    btn_list.at(3)->move(20,140);
    //btn_list.at(2)->resize(80,50);

    QFrame *line2 = new QFrame(this);
    line2->setGeometry(QRect(0, 180, 1024, 2));
    line2->setFrameShape(QFrame::HLine);
    line2->setFrameShadow(QFrame::Sunken);
    line2->raise();

    btn_list.at(4)->move(20,200);
    QLabel * opm_dBm_label = new QLabel(this);
    opm_dBm_label->setText("dBm");
    opm_dBm_label->move(430,210);
    opm_one_value_lcd_num  = new QLCDNumber(this);
    opm_one_value_lcd_num->move(350,200);
    opm_one_value_lcd_num->display("50");


    btn_list.at(5)->move(20,240);
    opm_value_lcd_num  = new QLCDNumber(this);
    opm_value_lcd_num->move(350,240);
    opm_value_lcd_num->display("50");
    QLabel * opm_value_label = new QLabel(this);
    opm_value_label->setText("dBm");
    opm_value_label->move(430,250);
    btn_list.at(6)->move(500,240);
    QFrame *line3 = new QFrame(this);
    line3->setGeometry(QRect(0, 280, 1024, 2));
    line3->setFrameShape(QFrame::HLine);
    line3->setFrameShadow(QFrame::Sunken);
    line3->raise();

    btn_list.at(7)->move(20,360);
    QFrame *line4 = new QFrame(this);
    line4->setGeometry(QRect(0, 390, 1024, 2));
    line4->setFrameShape(QFrame::HLine);
    line4->setFrameShadow(QFrame::Sunken);
    line4->raise();






    vfl_edit_group_box = new QGroupBox(this);
    vfl_edit_group_box->setTitle("光源设置");
    QStringList vflStrList;
    vflStrList<<"1310nm"<<"1550nm";
    for(int i = 0; i<vflStrList.count();i++)
    {
        QRadioButton *radioBtn = new QRadioButton(vfl_edit_group_box);
        radioBtn->setText(vflStrList.at(i));
        //out_range_radio_btn_list.append(radioBtn);
        radioBtn->move(i*100+10,25);
    }
    vfl_edit_group_box->move(20,400);
    vfl_edit_group_box->resize(600,50);




    btn_list.at(8)->move(20,460);
    btn_list.at(9)->move(200,460);

    line_edit_vfl_value = new QLineEdit(this);
    line_edit_vfl_value->move(400,460);
    line_edit_vfl_value->resize(80,25);
    btn_list.at(10)->move(500,460);


}

void SerialAnalyseData::opm_btn_handle()
{
    unsigned int count = 0;
    QPushButton* btn = qobject_cast<QPushButton*>(sender());
    QByteArray byte;
    if(btn == btn_list.at(0))
    {
        count = send_cmd_package(CONNECT_CMD);

    }
    else if(btn_list.at(1))
    {
        count = send_cmd_package(DISCONNECT_CMD);
    }
    else if(btn_list.at(2))
    {


    }
    QByteArray buf((char*)send_buf, count );
    emit serial_write_signal(buf);

}

unsigned char SerialAnalyseData:: sum_check_cmd(unsigned char* array,int len)
{
     int array_sum = 0;
     unsigned char i = 0;
     unsigned char array_avg = 0;
     printf("*(array+1) = %d   len = %d,\n",array[1],len);
     if(*(array+1) != len)
     {
          printf("device:sum_check_cmd is error\n");
          return 0;
     }

     for(i=0;i<*(array+1);i++)
     {
         array_sum = array_sum + *(array+i);
     }
     array_avg =(unsigned char) (array_sum%256);
     array_avg = ~array_avg + 1;

     return array_avg;
}

/*
void rec_buf_handle(unsigned char *buf,int len)
{
    unsigned char sumCheck = 0;
    if(len <3||buf[len]!=0x7d)return;
    sumCheck = sum_check_cmd(buf,buf[1]);
    if(sumCheck != buf[len-1])
    {
        printf("device:rec_buf_handle sum_check_cmd is error\n");
        return;
    }
    switch(buf[2])
    {
       case CONNECT_REPLY:
         break;
       case DISCONNECT_REPLY:
         break;
       case SET_PARAM_REPLY:
         break;
       case READ_PARAM_REPLY:
         break;
       case GET_ONCE_DATA_REPLY:
         break;
       case STOP_GET_DATA_REPLY:
         break;
       case SET_LIGHT_SWITCH_REPLY:
         break;
       case SET_LIGHT_POWER_REPLY:
         break;
       case READ_NET_SEQUENCE_REPLY:
         break;
       case SEARCH_SWITCH_REPLY:
         break;
       case WAVE_DISCERN_REPLY:
         break;
       case FREQUENCY_DISCERN_REPLY:
         break;

       default:
        break;
    }
}
*/












/**
* @projectName
* @brief         联机命令封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::connect_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
    return dataLen;
}
/**
* @projectName
* @brief         断开联机命令封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::disconnect_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
    return dataLen;
}

/**
* @projectName
* @brief         光功率计参数设置命令封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::set_param_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
//    buf[len++] = opm_measure_param.opm_type;
//    buf[len++] = opm_measure_param.sample_rate;
//    buf[len++] = opm_measure_param.opm_range;
//    dataLen += 3;
    return dataLen;
}
/**
* @projectName
* @brief         光功率计参数读取命令封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::read_param_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
//    buf[len++] = opm_measure_param.opm_type;
//    buf[len++] = opm_measure_param.sample_rate;
//    buf[len++] = opm_measure_param.opm_range;
//    dataLen += 3;
    return dataLen;
}
/**
* @projectName
* @brief         获取一次光功率值封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::get_once_data_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
//    buf[len++] = opm_read_value.wave_type;
//    dataLen += 1;
    return dataLen;
}
/**
* @projectName
* @brief         连续读取光功率值封包
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::get_data_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
//    buf[len++] = opm_read_value.wave_type;
//    buf[len++] = opm_read_value.speed;
//    dataLen += 2;
    return dataLen;
}
/**
* @projectName
* @brief         停止连续光功率获取
* @author        gaven
* @date          2020/03/13
*/
unsigned int SerialAnalyseData::stop_get_data_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
    return dataLen;
}
/**
* @projectName
* @brief         设置校准值
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::set_calibrate_value_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
//    buf[len++] = opm_check_value.wave_type;
//    buf[len++] = (unsigned char)opm_check_value.value_dBm>>8&0xff;
//    buf[len++] = (unsigned char)opm_check_value.value_dBm&0xff;
//    dataLen+=3;
    return dataLen;
}
/**
* @projectName
* @brief         设置稳定光源通断
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::set_light_switch_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
    return dataLen;
}
/**
* @projectName
* @brief         设置光源输出功率
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::set_light_power_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;
    return dataLen;
}
/**
* @projectName
* @brief         读取网线线序
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::read_net_sequence_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;


    return dataLen;
}
/**
* @projectName
* @brief         读取网线线长
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::read_net_len_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;


    return dataLen;
}
/**
* @projectName
* @brief         寻线功能开启或关闭
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::search_switch_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;


    return dataLen;
}
/**
* @projectName
* @brief
* @author        gaven
* @date          2020/03/10
*/
unsigned int SerialAnalyseData::wave_discern_cmd_packet(unsigned char *buf , int len)
{
    unsigned int dataLen = 0;


    return dataLen;
}
unsigned int SerialAnalyseData::frequency_discern_cmd_packet(unsigned char *buf , int len)
{
    int dataLen = 0;


    return dataLen;
}
unsigned int SerialAnalyseData::send_cmd_package(unsigned char cmd)
{
     int len = 0;
     int dataLen = 0;
     unsigned char sumCheck = 0;

     send_buf[len++] = 0x7b;
     send_buf[len++] = 0x00; //占位，空长度
     send_buf[len++] = cmd; //占位，空长度
     switch(cmd)
     {
         case  CONNECT_CMD:
            dataLen = connect_cmd_packet(send_buf,len);
          break;
         case  DISCONNECT_CMD:
            dataLen = disconnect_cmd_packet(send_buf,len);
          break;
         case  SET_PARAM_CMD:
            dataLen = set_param_cmd_packet(send_buf,len);
          break;
         case  READ_PARAM_CMD:
            dataLen = read_param_cmd_packet(send_buf,len);
          break;
         case GET_ONCE_DATA_CMD:
            dataLen = get_once_data_cmd_packet(send_buf,len);
          break;
         case GET_DATA_CMD:
            dataLen = get_data_cmd_packet(send_buf,len);
          break;
         case STOP_GET_DATA_CMD:
            dataLen = stop_get_data_cmd_packet(send_buf,len);
          break;
         case SET_CALIBRATE_VALUE_CMD:
            dataLen = set_calibrate_value_cmd_packet(send_buf,len);
          break;
         case SET_LIGHT_SWITCH_CMD:
            dataLen = set_light_switch_cmd_packet(send_buf,len);
          break;
         case SET_LIGHT_POWER_CMD:
            dataLen = set_light_power_cmd_packet(send_buf,len);
          break;
         case READ_NET_SEQUENCE_CMD:
            dataLen = read_net_sequence_cmd_packet(send_buf,len);
          break;
         case READ_NET_LEN_CMD:
            dataLen = read_net_len_cmd_packet(send_buf,len);
          break;
         case SEARCH_SWITCH_CMD:
            dataLen = search_switch_cmd_packet(send_buf,len);
          break;
         case WAVE_DISCERN_CMD:
            dataLen = wave_discern_cmd_packet(send_buf,len);
          break;
         case FREQUENCY_DISCERN_CMD:
            dataLen = frequency_discern_cmd_packet(send_buf,len);
          break;

        default:
           break;
     }
    len = dataLen + len;
    send_buf[1] = len;
    sumCheck = sum_check_cmd((unsigned char *)send_buf,len);
    send_buf[len++] = sumCheck;
    send_buf[len++] = 0x7d;
    return len;
}
